package com.zhonghui.modlules.user.eshopService.Impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.eshop.apiCode.EshopException;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopDomain.ShopUser;
import com.zhonghui.common.eshopEnum.BillDetailEnum;
import com.zhonghui.common.eshopEnum.PayTypeEnum;
import com.zhonghui.modlules.user.eshopDomain.UserExtract;
import com.zhonghui.modlules.user.eshopMapper.UserExtractMapper;
import com.zhonghui.modlules.user.eshopParam.UserExtParam;
import com.zhonghui.modlules.user.eshopService.UserBillService;
import com.zhonghui.modlules.user.eshopService.UserExtractService;
import com.zhonghui.modlules.user.eshopService.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Transactional(
        propagation = Propagation.SUPPORTS,
        readOnly = true,
        rollbackFor = {Exception.class}
)
public class UserExtractServiceImpl extends BaseServiceImpl<UserExtractMapper, UserExtract> implements UserExtractService {

    private UserExtractMapper userExtractMapper;
    private UserService userService;
    private UserBillService billService;

    public void userExtract(ShopUser userInfo, UserExtParam param) {
        BigDecimal extractPrice = userInfo.getBrokeragePrice();
        if (extractPrice.compareTo(BigDecimal.ZERO) <= 0) {
            throw new EshopException("提现佣金不足");
        } else {
            double money = Double.valueOf(param.getMoney());
            if (extractPrice.compareTo(BigDecimal.valueOf(money)) < 0) {
                throw new EshopException("提现佣金不足");
            } else if (money <= 0.0D) {
                throw new EshopException("提现佣金大于0");
            } else {
                double balance = NumberUtil.sub(extractPrice.doubleValue(), money);
                if (balance < 0.0D) {
                    balance = 0.0D;
                }

                UserExtract userExtract = new UserExtract();
                userExtract.setUid(userInfo.getUid());
                userExtract.setExtractType(param.getExtractType());
                userExtract.setExtractPrice(new BigDecimal(param.getMoney()));
                userExtract.setBalance(BigDecimal.valueOf(balance));
                if (StrUtil.isNotEmpty(param.getName())) {
                    userExtract.setRealName(param.getName());
                } else {
                    userExtract.setRealName(userInfo.getNickname());
                }

                if (StrUtil.isNotEmpty(param.getWeixin())) {
                    userExtract.setWechat(param.getWeixin());
                } else {
                    userExtract.setWechat(userInfo.getNickname());
                }

                String mark = "";
                if (PayTypeEnum.ALI.getValue().equals(param.getExtractType())) {
                    if (StrUtil.isEmpty(param.getAlipayCode())) {
                        throw new EshopException("请输入支付宝账号");
                    }

                    userExtract.setAlipayCode(param.getAlipayCode());
                    mark = "使用支付宝提现" + param.getMoney() + "元";
                } else if (PayTypeEnum.WEIXIN.getValue().equals(param.getExtractType())) {
                    if (StrUtil.isEmpty(param.getWeixin())) {
                        throw new EshopException("请输入微信账号");
                    }

                    mark = "使用微信提现" + param.getMoney() + "元";
                }

                this.userExtractMapper.insert(userExtract);
                ShopUser shopUser = new ShopUser();
                shopUser.setBrokeragePrice(BigDecimal.valueOf(balance));
                shopUser.setUid(userInfo.getUid());
                this.userService.updateById(shopUser);
                this.billService.expend(userInfo.getUid(), "佣金提现", BillDetailEnum.CATEGORY_1.getValue(), BillDetailEnum.TYPE_4.getValue(), money, balance, mark);
            }
        }
    }
}
